'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const {parseTime, parseNum} = require("../../utils");

module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, DECIMAL, TEXT } = app.Sequelize;
    const TravelShareOrder = app.model.define(
        'TravelShareOrder',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            travel_id: {type: INTEGER, allowNull: false, comment: '旅游产品ID'},
            travel_sku_id: {type: INTEGER, allowNull: true, comment: '旅游产品SKU ID'},
            travel_sku_stock_id: {type: INTEGER, allowNull: true, comment: '旅游产品SKU库存ID'},
            user_id: {type: INTEGER, allowNull: true, comment: '用户ID'},
            distributor_id: {type: INTEGER, allowNull: true, comment: '分销员ID'},
            prepay_amount: {
                type: DECIMAL(10,2), 
                defaultValue: '0.00', 
                comment: '预付金额',
                get() {
                    return parseNum(this.getDataValue('prepay_amount'));
                }
            },
            share_amount: {
                type: DECIMAL(10,2), 
                defaultValue: '0.00', 
                comment: '分销佣金',
                get() {
                    return parseNum(this.getDataValue('share_amount'));
                }
            },
            qrcode: {
                type: STRING(500),
                allowNull: true,
                comment: '分销二维码'
            },
            share_image_url: {
                type: STRING(500),
                allowNull: true,
                comment: '分销海报'
            },
            creator: {
                type: STRING(64),
                defaultValue: '0',
                comment: '创建者'
            },
            updater: {
                type: STRING(64),
                defaultValue: '0',
                comment: '更新者'
            },
            deleted: {
                type: TINYINT,
                defaultValue: 0,
                comment: '是否删除'
            },
            status: {
                type: STRING,
                defaultValue: 'pending',
                /**
                 * 'pending' COMMENT '待支付：订单已创建，但尚未完成支付',
                 * 'paid' COMMENT '已支付：订单已完成支付',
                 * 'refunding' COMMENT '退款中：订单正在进行退款流程',
                 * 'confirmed' COMMENT '已确认：旅游行程已确认',
                 * 'refunded' COMMENT '已退款：订单退款流程已完成',
                 * 'completed' COMMENT '已完成：旅游行程已完成',
                 * 'cancelled' COMMENT '已取消：订单已被取消'
                 * 'pending_verification' COMMENT '待确认': 订单已完成支付，等待确认行程
                 */
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'travel_share_order',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    TravelShareOrder.associate = function() {
        // 一对一关联旅游订单
        TravelShareOrder.hasOne(app.model.Business.TravelOrder, {
            foreignKey: 'travel_share_order_id',
            targetKey: 'id',
            as: 'travelOrder'
        })
        // 一对一关联用户
        TravelShareOrder.belongsTo(app.model.Business.User, {
            foreignKey: 'user_id',
            targetKey: 'id',
            as: 'user'
        })
        // 一对一关联分销发布者
        TravelShareOrder.belongsTo(app.model.Business.User, {
            foreignKey: 'distributor_id',
            targetKey: 'id',
            as: 'distributor'
        })
        // 一对一关联旅游产品
        TravelShareOrder.belongsTo(app.model.Business.Travel, {
            foreignKey: 'travel_id',
            targetKey: 'id',
            as: 'travel'
        })
        // 一对一关联旅游产品SKU
        TravelShareOrder.belongsTo(app.model.Business.TravelSku, {
            foreignKey: 'travel_sku_id',
            targetKey: 'id',
            as: 'travelSku'
        })
        // 一对一关联旅游产品SKU库存
        TravelShareOrder.belongsTo(app.model.Business.TravelSkuStock, {
            foreignKey: 'travel_sku_stock_id',
            targetKey: 'id',
            as: 'travelSkuStock'
        })
    }
    return TravelShareOrder;
};
